<!--
捐赠用途统计
-->
<template>
    <div v-if="isShow">
        <a-card size="small" :bordered="false" :hoverable="true">
            <!---------- 表格操作行 begin ----------->
            <a-row class="smart-table-btn-block">
                <div class="smart-table-operate-block" style="width: 90%;">
                    <a-button @click="down" type="primary" :loading="iconLoading">
                        <template #icon>
                            <UsbOutlined />
                        </template>
                        导出所有
                    </a-button>
                </div>
                <span class="smart-table-column-operate">
                    <TableOperator v-model="columns" :tableId="TABLE_ID_CONST.SYSTEM.EMPLOYEE" :refresh="queryData" />
                </span>
            </a-row>
            <!---------- 表格操作行 end ----------->

            <!---------- 表格 begin ----------->
            <a-table size="small" :dataSource="tableData" :columns="columns" rowKey="blfunduseid" bordered
                :loading="tableLoading" :pagination="false">
                <template #bodyCell="{ text, record, column }">
                    <template v-if="column.dataIndex === 'action'">
                        <div>
                            <a-button @click="viewDetail(record)" type="link">查看详情</a-button>
                        </div>
                    </template>
                </template>
            </a-table>
            <!-- 分页-->
            <div class="smart-query-table-page" style="margin: 15px 20px 5px 0px;">
                共{{total}}条
            </div>
        </a-card>
    </div>
    <!-- 包含捐赠用途的项目 -->
    <DonPurposeOperate v-else :passParams="fuPassword" @emitPass="changeShow" />
</template>
<script setup>
    import { reactive, ref, onMounted } from 'vue';
    import { message, Modal } from 'ant-design-vue';
    import { SmartLoading } from '/@/components/framework/smart-loading';
    import { getStatisticsApi } from '/@/api/infoStatistics/infoStatistics-api';
    import { PAGE_SIZE, PAGE_SIZE_OPTIONS } from '/@/constants/common-const';
    import { smartSentry } from '/@/lib/smart-sentry';
    import DonPurposeOperate from './modules/DonPurpose-operate.vue';
    import _ from 'lodash';
    import { TABLE_ID_CONST } from '/@/constants/support/table-id-const';
    import TableOperator from '/@/components/support/table-operator/index.vue';
    // ---------------------------- 表格列 ----------------------------

    const columns = ref([
        {
            title: '基金用途名称',
            dataIndex: 'blthreetype',
            align: 'center',
        },
        {
            title: '信息统计入口',
            dataIndex: 'action',
            align: 'center',
        },
    ]);

    // ---------------------------- 查询数据表单和方法 ----------------------------

    // 表格加载loading
    const tableLoading = ref(false);
    // 按钮加载loading
    const iconLoading = ref(false);
    // 表格数据
    const tableData = ref([]);
    // 总数
    const total = ref(0);
    // ====== 导出============= 
    async function down() {
        iconLoading.value = true;
        try {
            let queryResult = await getStatisticsApi.download();
            // 创建a标签
            const link = document.createElement('a')
            const url = window.URL.createObjectURL(new Blob([queryResult.data]))
            // 指定下载链接
            link.href = url
            // 指定下载文件名
            link.download = '捐赠用途统计表.xlsx'
            // 把a标签加到页面中
            document.body.appendChild(link)
            // 触发a标签下载
            link.click()
            // 下载完成移除元素
            document.body.removeChild(link)
            // 释放掉blob对象
            window.URL.revokeObjectURL(url)
            message.success(`导出成功！`)
        } catch (e) {
            smartSentry.captureError(e);
        } finally {
            iconLoading.value = false;
        }
    }

    // 数据列表
    async function queryData() {
        tableLoading.value = true;
        try {
            let queryResult = await getStatisticsApi.getUseList();
            tableData.value = queryResult.data;
            total.value = queryResult.data.length;
        } catch (e) {
            smartSentry.captureError(e);
        } finally {
            tableLoading.value = false;
        }
    }

    onMounted(queryData);
    // 控制父组件是否展示
    const isShow = ref(true);
    // 改变父组件展示状态
    function changeShow() {
        isShow.value = true;
    }
    // ---------------------------- 详情 ----------------------------
    const fuPassword = ref({});

    function viewDetail(data) {
        isShow.value = false;
        fuPassword.value = data;
    }


</script>